package com.sunshine.litemusic.fragment.recommend;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sunshine.litemusic.R;

import java.util.ArrayList;

/**
 * @author sunshine
 * @date 2020/5/29
 */
public class RecommendFragment extends Fragment {

    private RecommendViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        viewModel = new ViewModelProvider(this).get(RecommendViewModel.class);

        viewModel.getMusic();
        viewModel.getRecommendBeanMutableLiveData().observe(getViewLifecycleOwner(), new Observer<ArrayList<RecommendModel.RecommendBean>>() {
            @Override
            public void onChanged(ArrayList<RecommendModel.RecommendBean> recommendBeanArrayList) {
                if (null != recommendBeanArrayList){
                    RecyclerView recyclerView = getView().findViewById(R.id.recyclerView_music);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    recyclerView.setAdapter(new RecommendAdapter(recommendBeanArrayList));
                }
            }
        });
    }
}
